from tree import Tree
from node import Node
from tedtree import TEDTree
from generatetree import GenerateTree
from compare import Compare
import random

class Model:

    def __init__(self):
        self.changes = 20
        self.changedTrees = 20
        self.originals = 100
        self.randoms = 4000
        self.size = 100
        self.generator = GenerateTree(100, 20, 20, 4000, 100)
        self.TEDTree = TEDTree("C:\Users\Charles\Documents\jira-reporting\Thesis\Trees")

        return

    """
        Runs the entire model system to get the distance results to put
        through the classifier.
    """
    def runModel(self):
        self.ordered()
        self.unordered()
        self.real()
        return

    """
        This is for randomly generated ordered trees.
    """
    def ordered(self):
        self.generator.getOrdered()
        trees = []
        symbols = []
        for symbol in range(0,100):
            symbols.append(str(random.randint(-10000,10000)))
        for i in range(0, self.originals):
            size = random.randint(1,self.size)
            tree = self.generator.getTreeOrdered(size, symbols)
            original = tree
            print "Changed Trees"
            """
            for x in range(0, self.changedTrees):
                tree = self.generator.getChangedTree(tree)
                symbols.append(tree)
                trees.append(tree)
            """
            for x in range(0, self.changedTrees):
                tree = self.generator.getTreeOrdered(size, symbols)
                trees.append(tree)
            self.createComparisons(original, trees)
            self.getResultsBinary()
            trees = []
            original = ""
            print "Generate Tree"
        for y in range(0, self.randoms):
            size = random.randint(1, self.size)
            tree = self.generator.getTreeOrdered(size)
            trees.append(tree)
            self.createComparisons(original, trees)
        self.getResultsBinary()
        return trees

    def createComparisons(self, original, trees):
        count = 0
        for tree in trees:
            filenameOriginal = "test" + str(count) + "F.txt"
            self.TEDTree.createBinaryTree(original, filenameOriginal)
            filenameCompare = "test" + str(count) + "G.txt"
            self.TEDTree.createBinaryTree(tree, filenameCompare)
            count = count + 1

    def getResultsBinary(self):
        results = []
        trees = self.TEDTree.findFilesBinary()
        for comparison in trees.keys():
            original = trees[comparison]["original"]
            compare = trees[comparison]["compare"]
            results.append(Compare(original, compare).rotation_distance())
            print results
        print results
        return
    
    """
        This is for randomly generated unordered trees.
    """
    def unordered(self):
        trees = self.generator.getUnordered()
        return

    """
        This will take care of the JSON trees which I find. Might want
        to also look at xml.
    """
    def real(self):
        return

    """
        This will take care of building all of the randomly trees that we need
        for testing of the system.
    """
    def buildTrees(self):
        return
